import Common from '../../common/common.js';
import BaseWin from '../base/BaseWin.js';
import ServiceNet from "../../servicenet.js";
import TimNet from "../../timnet.js";
import Menu from "./Menu.js";

/***
 * 登录
 ***/
function LoginWin () {
    //人员列表对象
    BaseWin.call(this);
    this.element.className += " loginWin";

}
let prototype = Object.create(BaseWin.prototype);
prototype.constructor = LoginWin;
LoginWin.prototype  = prototype;

LoginWin.prototype.initBox = function(){
     let box = Common.tool.createElement({
        childNodes:[
            {label:"input",tag:"name",props:{type:"text",placeholder:"用户ID"}},
            {label:"input",tag:"password",props:{type:"password",placeholder:"密码"}},
            {label:"button",tag:"button",props:{innerText:"登录"}},
        ]
    });
    this.element.name = box.name;
    this.element.password = box.password;

    this.element.win.box.appendChild(box);
    this.element.win.removeChild(this.element.win.close);
    this.element.win.removeChild(this.element.win.buts);

    Common.tool.addHandle(box.button,"click",()=>{
        this.submit();
    });
}

LoginWin.prototype.submit = function(){

    let userIdVal = this.element.name.value;
    let passwordVal = this.element.password.value;

    //通过服务器登录拿到密钥userSig
    ServiceNet.login(userIdVal, passwordVal, (r) => {
        //使用userSig和userId登录腾讯云SDK
        TimNet.login(userIdVal, r.userSig, () => {
            // 获取个人信息
            this.cancel();
            TimNet.getMyProfile((d) =>
            {
                Common.cache.myProfile = d;
                Menu.updateAvatar(d.avatar);
            });
        });
    }, (e) => {
        alert(e.sysMsg);
    });
}


/***
 * 修改我的信息
 ***/
function EditMyWin () {
    //人员列表对象
    BaseWin.call(this);
    this.element.className += " editMyWin";
    this.form
}
let prototype2 = Object.create(BaseWin.prototype);
prototype2.constructor = EditMyWin;
EditMyWin.prototype  = prototype2;

EditMyWin.prototype.initBox = function(){

    let myProfile = Common.cache.myProfile;
    const form = Common.tool.createElement({
        label:"form",
        props:{name:"editMyWinForm"},
        childNodes:[
            {
                label:"table",
                childNodes:[
                    {
                        label:"tr",
                        childNodes:[
                            { label:"th", props:{innerText:"头像"}  },
                            { label:"td", childNodes:[{label:"input",props:{type:"text",name:"avatar",placeholder:"图片 url",maxLength:500,value:myProfile.avatar}} ]}
                        ]
                    },
                    {
                        label:"tr",
                        childNodes:[
                            { label:"th", props:{innerText:"昵称"}  },
                            { label:"td", childNodes:[{label:"input",props:{type:"text",name:"nick",maxLength:30,value:myProfile.nick}} ]}
                        ]
                    },
                    {
                        label:"tr",
                        childNodes:[
                            { label:"th", props:{innerText:"性别"}  },
                            { label:"td", childNodes:[
                                {label:"input",props:{type:"radio",name:"gender",checked:myProfile.gender==TIM.TYPES.GENDER_MALE,value:TIM.TYPES.GENDER_MALE}},
                                {label:"span",props:{innerText:"男"}},
                                {label:"input",props:{type:"radio",name:"gender",checked:myProfile.gender==TIM.TYPES.GENDER_FEMALE,value:TIM.TYPES.GENDER_FEMALE}},
                                {label:"span",props:{innerText:"女"}},
                                {label:"input",props:{type:"radio",name:"gender",checked:myProfile.gender==TIM.TYPES.GENDER_UNKNOWN,value:TIM.TYPES.GENDER_UNKNOWN}},
                                {label:"span",props:{innerText:"未知"}}
                             ]}
                        ]
                    },
                    {
                        label:"tr",
                        childNodes:[
                            { label:"th", props:{innerText:"个性签名"}  },
                            { label:"td", childNodes:[{label:"input",props:{type:"text",name:"selfSignature",maxLength:500}} ]}
                        ]
                    },
                    {
                        label:"tr",
                        childNodes:[
                            { label:"th", props:{innerText:"权限"}  },
                            { label:"td", childNodes:[
                                    {label:"input",props:{type:"radio",name:"allowType",checked:myProfile.allowType==TIM.TYPES.ALLOW_TYPE_ALLOW_ANY,value:TIM.TYPES.ALLOW_TYPE_ALLOW_ANY}},
                                    {label:"span",props:{innerText:"允许"}},
                                    {label:"input",props:{type:"radio",name:"allowType",checked:myProfile.allowType==TIM.TYPES.ALLOW_TYPE_DENY_ANY,value:TIM.TYPES.ALLOW_TYPE_DENY_ANY}},
                                    {label:"span",props:{innerText:"禁止"}},
                                ]}
                        ]
                    }
                ]
            }
        ]
    });

    this.form = form;
    this.element.win.box.appendChild(form);
}

EditMyWin.prototype.submit = function(){
    let editMyWinForm = document.forms["editMyWinForm"];
    let formData = new FormData(editMyWinForm);

    TimNet.updateMyProfile({
        nick:formData.get("nick"),
        avatar: formData.get("avatar"),
        gender: formData.get("gender"),
        selfSignature:formData.get("selfSignature"),
        allowType: formData.get("allowType")
    },(data)=>{
        Common.cache.myProfile = data;
        Menu.updateAvatar(data.avatar);
        this.cancel();
    })
};

export {EditMyWin,LoginWin} ;
